<{capture name="header"}>
<{css app="ome" src="ome.css"}>
<{css app="ome" src="style.css"}>
<{script src="coms/autocompleter.js" app="desktop"}>
<{script src="coms/pager.js" app="desktop"}>
<{/capture}>
<style>
    .notice-inline.error  {background-image:none;margin-left:25px}    
</style>
<form method="post" id="fm1" action="index.php?app=wms&ctl=admin_expressapapi&act=doSave">
  <input type="hidden" size="32"  id="id" name="id" value="<{$expressapapi.id}>" />
    <div class="tableform">
      <h3>新建应付快递费过数</h3>
      <div class="division">
        <h5>基本信息</h5>
        <table border="0" cellspacing="0" cellpadding="0" class="gridlist">
        <thead>
            <tr>
                <td width="10%" align="right" nowrap="nowrap">
                    应收单号：
                </td>
                <td colspan="3">
                    <span id="exigence" style="color:red; display:none">！</span>
                    <input type="text" size="32"  id="purchase_name" name="purchase_name" value="<{$expressapapi.apapi_bn}>" />
                </td>
            </tr>
            <tr>
                <td width="10%" align="right" nowrap="nowrap">
                    开始日期：
                </td>
                <td  width="20%">
                    <span id="exigence" style="color:red; display:none">！</span>
                    <{input type="date" name="begin_date" vtype="required" id="begin_date" value="{$expressapapi.begin_date}" }>
                </td>
                <td width="10%" align="right" nowrap="nowrap">
                    AX入账日期：
                </td>
                <td  width="20%">
                    <span id="exigence" style="color:red; display:none">！</span>
                    <{input type="date" name="booked_time" vtype="required" id="booked_time" value="{$expressapapi.booked_time}" }>
                </td>
            </tr>
            <tr>
                <td width="10%" align="right" nowrap="nowrap">
                    结束日期：
                </td>
                <td width="20%">
                    <span id="exigence" style="color:red; display:none">！</span>
                    <{input type="date" name="end_date" vtype="required" id="end_date" value="{$expressapapi.end_date}" }>
                </td>
                <td width="10%" align="right" nowrap="nowrap">
                    过数日期：
                </td>
                <td width="20%">
                    <span id="exigence" style="color:red; display:none">！</span>
                    <{input type="date" name="inter_time" vtype="required" id="inter_time" value="{$expressapapi.inter_time}" }>
                </td>
            </tr>
            <tr>
                <td width="10%" align="right" nowrap="nowrap">
                    结算方式：
                </td>
                <td colspan="3">
                    <span id="exigence" style="color:red; display:none">！</span>
                        <select name="JSType" id="JSType">
                            <{foreach from=$jstypelist key=key item=val}>
                            <option value="<{$val.JSType}>" <{if $val.JSType ==$expressapapi.JSType }>selected<{/if}>  >
                            <{$val.name}>
                            </option>
                            <{/foreach}>
                        </select>
                </td>
            </tr>

          </thead>
      </table>
      <h5 style="margin-top:10px;">过数汇总</h5>

      <{button label="生成" id="purchase-find-btn" }>
      <table class="gridlist" id="purchase_table"  style="margin:4px 0;">
          <thead>
            <tr>
            <th><input type="checkbox" class='selectall' onclick="this.blur()"></th>    
              <th>供应商</th>
              <th>AX供应商</th>
              <th>客户</th>
              <th>客户名称</th>
              <th>结算方式</th>
              <th>料号</th>
              <th>料品</th>
              <th>单位</th>
              <th>销售数量</th>
              <th>销售金额</th>
              <th>应付运费</th>
              <th>服务费</th>
              <th style="width:30px;">删除</th>
            </tr>
          </thead>
          <tbody id="dataNode">
              <tr>
                  <td colspan="12" style="padding:0;"><div class="note" style="margin:0;"> 暂无明细信息 </div></td>
              </tr>
          </tbody>
      </table>
          <div align="right">
              <{button label="删除选中" id="purchase-del-btn"}> &nbsp;
              <{button type="button" id="purchase-delall-btn" label="全部删除" }>
          </div>
      </div>  
      <table width="100%" border="0" cellpadding="0" cellspacing="0" style="margin-top:10px;">
          <tr>
              <td width="10%" align="right">备注:</td>
              <td align="left"><textarea id="memo" maxth="255" rows="3" cols="80%" name="memo" type="textarea" value="<{$expressapapi.memo}>"></textarea></td>
          </tr>
          <tr>
              <td align="right">经办人:</td>
              <td align="left"><input name="operator" type="text" value="<{$expressapapi.operator}>" size="15" vtype="required"/></td>
          </tr>
      </table>
      <div id="cc" class="noprint table-action">
          <{button type="button" id="purchase-save-btn" label="保 存"}> &nbsp;
          <{button type="button" class="btn-secondary" id="return-btn" label="关 闭" onclick="javascript:void(window.close());"}>
      </div>
    </div>
</form>
<script>
(function(){
var visiTips = new Tips({
    onShow:function(tip,el){
      el.addClass('active');
      tip.setStyle('display','block');
    }
});

  var callurl='index.php?app=wms&ctl=admin_expressapapi&act=getdeliverys',store=[];
  var options={
    'getVar':'bn',
    'fxOptions':false,
    callJSON:function(){return window.autocompleter_json;},
    injectChoice:function(json){
      var choice = new Element('li', {'html': this.markQueryValue(json[this.options.getVar])});
      choice.store('_data',json);
      choice.inputValue = json[this.options.getVar];
      this.addChoiceEvents(choice).inject(this.choices);
    },
    onHide:function(ipt){
      if(!this.selected || ipt.value=='')return;
      var json=this.selected.retrieve('_data');
      json=$splat(json);
      init(json);
      MessageBox.success('加载商品成功!!');
        },
        onFocus:function(ipt){
      ipt.value='';
    }
    };

    var tpl='<tr key="{product_id}" id="product_{product_id}" title="点击选取/反选此条记录">'+
        '<td><input type="checkbox" class="product_select"></td>'+
        '<td><input type="hidden" value="{product_id}" name="delivery_ID[{product_id}]" key="num"  tname="at[_PRIMARY_]" size="20">{AXsupplierCode}</td>'+
        '<td style="display:none;"><input type="hidden" value="{AXsupplierCode}" name="delivery_ID[{product_id}][AXsupplierCode]" key="num"  tname="at[_PRIMARY_]" size="20"></td>'+
        '<td style="display:none;"><input type="hidden" value="{AXsupplierName}" name="delivery_ID[{product_id}][AXsupplierName]" key="num"  tname="at[_PRIMARY_]" size="20"></td>'+
        '<td><input type="hidden" value="{AXCustomer}" name="delivery_ID[{product_id}][AXCustomer]" key="num"  tname="at[_PRIMARY_]" size="20">{AXCustomer}</td>'+
        '<td><input type="hidden" value="{customername}" name="delivery_ID[{product_id}][customername]" key="num"  tname="at[_PRIMARY_]" size="20">{customername}</td>'+
        '<td><input type="hidden" value="{JSType}" name="delivery_ID[{product_id}][JSType]" key="num"  tname="at[_PRIMARY_]" size="20">{JSType}</td>'+
        '<td><input type="hidden" value="{bn}" name="delivery_ID[{product_id}][bn]" key="num"  tname="at[_PRIMARY_]" size="20">{bn}</td>'+
        '<td><input type="hidden" value="{product_name}" name="delivery_ID[{product_id}][product_name]" key="num"  tname="at[_PRIMARY_]" size="20">{product_name}</td>'+
        '<td><input type="hidden" value="{unit}" name="delivery_ID[{product_id}][unit]" key="num"  tname="at[_PRIMARY_]" size="20">{unit}</td>'+
        '<td><input type="hidden" value="{saleqty}" name="delivery_ID[{product_id}][saleqty]" key="num"  tname="at[_PRIMARY_]" size="20">{saleqty}</td>'+
        '<td><input type="hidden" value="{salemny}" name="delivery_ID[{product_id}][salemny]" key="num"  tname="at[_PRIMARY_]" size="20">{salemny}</td>'+
        '<td><input type="hidden" value="{delivery_cost_actual}" name="delivery_ID[{product_id}][delivery_cost_actual]" key="num"  tname="at[_PRIMARY_]" size="20">{delivery_cost_actual}</td>'+
        '<td><input type="hidden" value="{servicefee}" name="delivery_ID[{product_id}][servicefee]" key="num"  tname="at[_PRIMARY_]" size="20">{servicefee}</td>'+
        '  <td><{img src="bundle/delecate.gif" app="desktop" key="state" class="pointer btn-delete-item"}></td>'+
        '</tr>';

    new Request({url:'index.php?app=wms&ctl=admin_expressapapi&act=getEditProducts&p[0]='+<{$expressapapi.id}>,
    onComplete:function(rs){
        rs=JSON.decode(rs);
        store.combine(rs);
        createProduct(store);
    }}).send();

    $('purchase-find-btn').addEvent('click',function(e)
    {   
        var begin_date=$('begin_date').value;
        var end_date=$('end_date').value;
        var JSType=$('JSType').value;
        new Request({url:'index.php?app=wms&ctl=admin_expressapapi&act=getdetails&begin_date='+begin_date+"&end_date="+end_date+"&JSType="+JSType,
        onComplete:function(rs){
            rs=JSON.decode(rs);
            store.combine(rs);
            createProduct(store);
            $('purchase-find-btn').disabled="disabled";
        }}).send();

    });


    var pag;
    function emptyData(){
        var noData='<tr>'+
            '<td colspan="8" style="padding:0;"><div class="note" style="margin:0;"> 暂无发货明细信息 </div></td>'+
            '</tr>';
        $('dataNode').set('html',noData);
    }
    var _next_key = next_key =  product_id = last_key = null;    
    function createProduct(data){
    pag=new PageData(tpl,data,{'updateMain':$('dataNode'),'pageNum':10000,
            'format': function(d) {
            },
        'onShow':function(){
                var _this=this;
                $$('#dataNode tr').each(function(item,i){
                    item.addEvent('click',function(e){
                        //e.stop(); 
                      if(_next_key){
                            $E('#product_'+_next_key).set('style','');
                            delete _next_key;//马上删除这个临时变量
                         }                       
                        this.toggleClass('selected');
                        _this.selectData(this.get('key')).selected = _this.selectData(this.get('key')).selected && _this.selectData(this.get('key')).selected === true ? false : true;
                        var selectflag = _this.selectData(this.get('key')).selected;
                        var selectproduct = item.getElement('.product_select');
                        if (selectflag)
                        {
                            selectproduct.set('checked','checked');
                        }else{
                            selectproduct.set('checked','');
                        }
                        
                    });
                   
                    item.getElement('.btn-delete-item').addEvent('click',function(e){
                        e.stop();
                        product_id = 'product_'+item.get('key');
                        _next_key = $$('#'+product_id+'+tr').get('key');
                        //next_key = 'product_'+_next_key;                        
                        if(_this.selectData(item.get('key')) && confirm('确定要删除 '+ _this.selectData(item.get('key'))['delivery_bn'] +' 吗？')){
                          compute(item);
                          _this.delData(item.get('key'));    
                          if(isNaN(_next_key) == false){
                            next_key = 'product_'+_next_key; 
                              $E('#'+next_key).set('style','background:none repeat scroll 0 0 #6699FF;color:#FFFFCC');            
                          }else{
                            _next_key = null;
                          }
                          product_id =  next_key = null;
                        }
                        if(!$E('#dataNode tr')) emptyData();
                    });
                    item.getElement('input[tname^=at]').addEvent('enter',function(e){
                        $E('#pfba input').focus();
                    });
                });
                
                if(this.data.length) {
                    $E('#dataNode input[key^=num]').focus();
                
                }

            }
        });
    }
    $E('.selectall').addEvent('click',function(e){
    
       if (this.checked)
        {
            $ES('.product_select').set('checked','checked');
            $$('.product_select').each(function(item){
                protr = item.getParent('tr');
                product_id = protr.get('key');
                protr.addClass('selected');
                pag.selectData(product_id).selected=true;
            
            });
        }else{
            $ES('.product_select').set('checked','');
            $$('.product_select').each(function(item){
                protr = item.getParent('tr');
                product_id = protr.get('key');
                protr.removeClass('selected');
                pag.selectData(product_id).selected=false;
            
            });
        }
        
    
    
    });

    $('purchase-delall-btn').addEvent('click',function(e){
        if(!pag||!pag.data)return;
        var delarr=[];
    pag.data.each(function(d){
             delarr.push(d['product_id']);
        });
        if(delarr.length && confirm('确认删除全部发货明细吗？')){
            delProduct(pag,delarr);
            emptyData();
        }
    });

    $('purchase-del-btn').addEvent('click',function(e){
        if(!pag||!pag.data)return;
        var delarr=[];
        pag.data.each(function(d,i){
      if(pag.selectData(d['product_id']).selected && pag.selectData(d['product_id']).selected === true) delarr.push(d['product_id']);
    });
        //获取批量删除中的最后一个节点
        last_key =    $$('.selected').getLast();
        last_key = last_key.get('key');
        if(delarr.length){
          if(confirm('确认删除选定的发货明细吗？')){
            product_id = 'product_'+last_key;
          //找到本次批量删除最后一条记录下面的那条记录
            _next_key = $$('#'+product_id+'+tr').get('key'); 
            delProduct(pag,delarr);
            if(isNaN(_next_key) == false){
              next_key = 'product_'+_next_key; 
                //对本次批量删除最后一条记录下面的那条记录，颜色加深提示
                $E('#'+next_key).set('style','background:none repeat scroll 0 0 #6699FF;color:#FFFFCC');            
            }else{
              _next_key = null;
            }
            product_id = next_key=last_key = null;
          }
            if(!$E('#dataNode tr')) emptyData();
        }
        else MessageBox.error("请先选择欲删除的发货明细！");
  });
    function compute(e){
    }

    function init(rs){
        var tmparr=findProduct(rs,'product_id');
         store.unshift.apply(store,tmparr);
         createProduct(store);
    }
    function findProduct(arr,PRIMARY){
        if(!store.length)return arr;
        store.each(function(a){
            arr.each(function(b){
                if(a[PRIMARY]==b[PRIMARY])arr.erase(b);
            });
        });
        return arr;
    }
    function delProduct(obj,arr){
        arr.each(function(d){obj.delData(d);});
    }
    
    $('purchase-save-btn').addEvent('click',function(e){
        var _this=this;
        var form=this.getParent('form');
        if(pag){
            var data=pag.toHideInput($('dataNode').getElement('tr'));
            form.store('target',{extraData:data,
                onRequest:function(){
                    _this.disabled=true;
                },
                onComplete:function(jsontext){
                    try{
                        var json = JSON.decode(jsontext);
                        if (typeof(json.error)!='undefined'){
                            _this.disabled=false;
                        }else{
                            _this.disabled=true;
                            if(opener.finderGroup['<{$env.get.finder_id}>']) opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(100,opener.finderGroup['<{$env.get.finder_id}>']);
                            setTimeout('window.close()',200);
                        }
                    }catch(e){}
                }
            });
        }
        form.fireEvent('submit',e);
    });
})();





</script>
